<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Calc</title>
</head>
<body>
<div style="text-align: center;"/>
<h1>
    Calculate N!
</h1>
<!--*.jsp-->
<form action="clacN.jsp"/>
<input type="number" name="num"/><br/><br/>
<input type="submit"/><br/><br/>
<form/>

<%!
    private int n = -1;
%>
<%!
    public int fac(int n) {
        if (n < 0) {
            return n;
        } else if (n == 0) {
            return 1;
        } else {
            int product = 1;
            for (int i = 1; i <= n; i++) {
                product *= i;
                if (product < 0) {
                    break;
                }
            }
            return product;
        }
    }
%>
<%
    try {
        int n = fac(Integer.parseInt(request.getParameter("num")));
        if (n < 0) {
            out.print("<h2>invalid</h2>");
        } else {
            out.print("<h2>"+n+"</h2>");
        }
    } catch (Exception e) {
        //ignore java.lang.NumberFormatException: null
    }
%>

</body>
</html>
